﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Linq.Expressions;
using P3SS.FailedMail;

namespace P3SS.ConfigUtil
{
	public partial class frmFailedMail : Form
	{
		private bool isSelectedMessageModified;
		private int lastSelectedIndex;
		private bool hasModifiedEmails;
		private string[] smtpServers;

		public List<FailedMailItem> MailItems { get; set; }

		public frmFailedMail(List<FailedMailItem> inMailItems, string[] inSmtpServers)
		{
			InitializeComponent();
			MailItems = inMailItems;
			isSelectedMessageModified = false;
			lastSelectedIndex = -1;
			hasModifiedEmails = false;
			smtpServers = inSmtpServers;
		}

		private void frmFailedMailAccount_Load(object sender, EventArgs e)
		{
			lstMail.Items.Clear();
			lstMail.Items.AddRange(MailItems.ToArray());

			if (lstMail.Items.Count == 0)
			{
				MessageBox.Show("There are no failed emails on this server", "No Failed Emails", MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			if (lstMail.SelectedIndices.Count > 0)
			{
				if (MessageBox.Show("Are you sure you wish to delete these emails? You can not undo this action...", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
				{
					MailItems.RemoveAll(p => lstMail.SelectedItems.Contains(p));
					
					lstMail.Items.Clear();
					lstMail.Items.AddRange(MailItems.ToArray());
					lastSelectedIndex = -1;
					hasModifiedEmails = true;
				}
			}
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			if (MessageBox.Show("Are you sure you wish to save changes? You can not undo this action...\r\n(NOTE: The message must be in the correct format, otherwise it may fail to send.)", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
			{
				((FailedMailItem)lstMail.SelectedItem).MailItem = txtMessage.Text;
				((FailedMailItem)lstMail.SelectedItem).EmailAddress = txtEmailAddress.Text;
				((FailedMailItem)lstMail.SelectedItem).SMTPServer = cmbSmtpServer.SelectedItem.ToString();
				isSelectedMessageModified = false;
				hasModifiedEmails = true;
			}
		}

		private void btnRevert_Click(object sender, EventArgs e)
		{
			txtMessage.Text = ((FailedMailItem)lstMail.SelectedItem).MailItem;
			isSelectedMessageModified = false;
		}

		private void txtMessage_TextChanged(object sender, EventArgs e)
		{
			isSelectedMessageModified = false;
			if (lstMail.SelectedIndices.Count == 1)
			{
				isSelectedMessageModified |= ((FailedMailItem)lstMail.SelectedItem).MailItem != txtMessage.Text;
				isSelectedMessageModified |= ((FailedMailItem)lstMail.SelectedItem).EmailAddress != txtEmailAddress.Text;
				if (cmbSmtpServer.Items.Count > 0)
				{
					isSelectedMessageModified |= ((FailedMailItem)lstMail.SelectedItem).SMTPServer != cmbSmtpServer.SelectedItem.ToString();
				}
			}
		}

		private void lstMail_SelectedIndexChanged(object sender, EventArgs e)
		{
			if ((lastSelectedIndex > -1) && (isSelectedMessageModified) && (MessageBox.Show("The changes made to the message have not been saved yet.\r\n\r\nWould you like to save now?\r\n(NOTE: The message must be in the correct format, otherwise it may fail to send.)", "Save now?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes))
			{
				((FailedMailItem)lstMail.Items[lastSelectedIndex]).MailItem = txtMessage.Text;
			}

			cmbSmtpServer.Items.Clear();

			switch (lstMail.SelectedIndices.Count)
			{
				case 0:
					// nothing selected
					btnDelete.Enabled = false;
					btnSave.Enabled = false;
					btnRevert.Enabled = false;
					txtFailure.Text = "";
					txtMessage.Text = "";
					txtEmailAddress.Text = "";
					lastSelectedIndex = -1;
					break;
				case 1:
					// one selected
					FailedMailItem selectedMailItem = (FailedMailItem)lstMail.SelectedItem;
					txtMessage.Text = selectedMailItem.MailItem;
					txtFailure.Text = selectedMailItem.FailureReason;
					txtEmailAddress.Text = selectedMailItem.EmailAddress;
					lastSelectedIndex = lstMail.SelectedIndex;
					isSelectedMessageModified = false;
					cmbSmtpServer.Items.AddRange(smtpServers);
					if (!smtpServers.Contains(selectedMailItem.SMTPServer))
					{
						cmbSmtpServer.Items.Add(selectedMailItem.SMTPServer);
					}
					cmbSmtpServer.SelectedItem = selectedMailItem.SMTPServer;
					btnDelete.Enabled = true;
					btnSave.Enabled = true;
					btnRevert.Enabled = true;
					break;
				default:
					// multiple (or negative) selected
					btnDelete.Enabled = true;
					btnSave.Enabled = false;
					btnRevert.Enabled = false;
					txtFailure.Text = "";
					txtMessage.Text = "";
					txtEmailAddress.Text = "";
					lastSelectedIndex = -1;
					break;
			}
		}

		private void btnExit_Click(object sender, EventArgs e)
		{
			closeForm();
		}

		private void closeForm()
		{
			if (hasModifiedEmails)
			{
				this.DialogResult = DialogResult.OK;
			}
			else
			{
				this.DialogResult = DialogResult.Cancel;
			}
		}

		private void frmFailedMail_FormClosing(object sender, FormClosingEventArgs e)
		{
			if ((e.CloseReason == CloseReason.UserClosing) || (e.CloseReason == CloseReason.None))
			{
				closeForm();
			}
		}
	}
}
